package com.go.where.family.component.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.go.where.family.component.StarPhotoComponent;
import com.go.where.family.constants.Constant;
import com.go.where.family.constants.TransmissionCode;
import com.go.where.family.exception.ServerDisponseException;
import com.go.where.family.pojo.AlbumInfo;
import com.go.where.family.pojo.StarPhotoInfo;
import com.go.where.family.scoket.Head;
import com.go.where.family.service.AlbumService;
import com.go.where.family.service.StarPhotoService;
import com.go.where.family.utils.CommonUtil;
import com.go.where.family.vo.StarPhotoVo;
import com.google.gson.Gson;

@Component("starPhotoComponent")
public class StarPhotoComponentImpl implements StarPhotoComponent {

	private static final Logger log = Logger
			.getLogger(StarPhotoComponentImpl.class);
	private Gson gson;
	@Resource(name = "starPhotoService")
	private StarPhotoService starPhotoService;
	@Resource(name = "albumService")
	private AlbumService albumService;

	public StarPhotoComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public List<StarPhotoInfo> queryList(String body, Head head) {
		StarPhotoVo vo = gson.fromJson(body, StarPhotoVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("pageStart", pageStart);
			map.put("pageEnd", pageEnd);
			map.put("isDelete", Constant.IS_NOT_DELETE);
			map.put("isElect", "1");
			map.put("albumId", vo.getAlbumId());
			if (StringUtils.isNotBlank(vo.getOrderBy())) {
				if (Constant.ORDER_TYPE_ASC.equals(vo.getOrderType())) {
					map.put("page_orderByAsc", vo.getOrderBy());
				} else {
					map.put("page_orderByDesc", vo.getOrderBy());
				}
			}
			List<StarPhotoInfo> list = starPhotoService.queryList(map);
			if (list != null && !list.isEmpty()) {
				for (StarPhotoInfo star : list) {
					if (StringUtils.isNotBlank(star.getAlbumPath())) {
						star.setAlbumPath(CommonUtil.getUrlPrefix()
								+ star.getAlbumPath());
					}
					if (StringUtils.isNotBlank(star.getPhotoPath())) {
						star.setPhotoPath(CommonUtil.getUrlPrefix()
								+ star.getPhotoPath());
					}
				}
				return list;
			} else {
				return new ArrayList<StarPhotoInfo>();
			}
		} else {
			log.info("requestId:" + head.getRequestId()
					+ ",获取本月票选大明星照片列表信息时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public StarPhotoInfo queryStarPhotoDetail(String body, Head head) {
		StarPhotoVo vo = gson.fromJson(body, StarPhotoVo.class);
		if (vo != null) {
			String id = vo.getId();
			StarPhotoInfo spi = starPhotoService.queryStarPhotoDetail(id);
			if (spi != null) {
				if (StringUtils.isNotBlank(spi.getAlbumPath())) {
					spi.setAlbumPath(CommonUtil.getUrlPrefix()
							+ spi.getAlbumPath());
				}
				if (StringUtils.isNotBlank(spi.getPhotoPath())) {
					spi.setPhotoPath(CommonUtil.getUrlPrefix()
							+ spi.getPhotoPath());
				}
				return spi;
			} else {
				log.info("requestId:" + head.getRequestId()
						+ ",查询大明星详情信息时,没有查询到数据,ID：" + id);
				throw new ServerDisponseException(TransmissionCode.NO_DATA
						.getCode(), TransmissionCode.NO_DATA.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取大明星详情信息时，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public boolean updateSupport(String body, Head head) {
		StarPhotoVo vo = gson.fromJson(body, StarPhotoVo.class);
		if (vo != null) {
			String id = vo.getId();
			AlbumInfo album = albumService.queryAlbumDetail(id);
			if (album != null) {
				Integer supportNum = album.getSupportNum();
				if ("1".equals(vo.getSupportOrNot())) {
					supportNum = CommonUtil.add(supportNum, 1);
				} else if ("0".equals(vo.getLikeOrNot())) {
					supportNum = CommonUtil.sub(supportNum, 1);
				}
				AlbumInfo sp = new AlbumInfo();
				sp.setId(id);
				sp.setSupportNum(supportNum);
				int re = albumService.update(sp);
				if (re > 0) {
					return true;
				} else {
					return false;
				}
			} else {
				log.info("requestId:" + head.getRequestId()
						+ ",更新票选支持数时,没有查询到数据,ID：" + id);
				throw new ServerDisponseException(TransmissionCode.NO_DATA
						.getCode(), TransmissionCode.NO_DATA.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",更新票选支持数，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public List<AlbumInfo> queryAlbumList(String body, Head head) {
		StarPhotoVo vo = gson.fromJson(body, StarPhotoVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("pageStart", pageStart);
			map.put("pageEnd", pageEnd);
			map.put("isDelete", Constant.IS_NOT_DELETE);
			// if (vo.getMonth() == null) {
			// String s = DateUtil.format(new Date(), "yyyy-MM");
			// map.put("month", s);
			// } else {
			map.put("month", vo.getMonth());
			// }
			Long totalSupport = albumService.queryTotalSupport(map);
			List<AlbumInfo> list = albumService.queryList(map);
			if (list != null && !list.isEmpty()) {
				for (AlbumInfo album : list) {
					if (totalSupport != null && totalSupport.intValue() != 0) {
						Double rate = CommonUtil.div(album.getSupportNum(),
								totalSupport).doubleValue();
						album.setSupportRate(rate);
					}
					if (StringUtils.isNotBlank(album.getAlbumPath())) {
						album.setAlbumPath(CommonUtil.getUrlPrefix()
								+ album.getAlbumPath());
					}
				}
				return list;
			} else {
				return new ArrayList<AlbumInfo>();
			}
		} else {
			log.info("requestId:" + head.getRequestId()
					+ ",获取本月票选大明星相册列表信息时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}

	}
}
